package com.kongwg.exer;

/**
 * 练习：创建两个綫程，一個线程遍历一百以内的偶数，一个綫程遍历一百以内的基数；
 *
 * @author kongwg
 * @create 2020-12-04 16:09
 */
public class ThreadDemo {
    public static void main(String[] args) {
//            MyThread1 m1 = new MyThread1();
//            m1.start();
//            MyThread2 m2 = new MyThread2();
//            m2.start();
        //拆功能键Thread类的匿名对象方式
        new MyThread1(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }

                }
            }
        }.start();

        new MyThread2(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 != 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }

                }
            }
        }.start();

        Thread.currentThread().setName("主线程-");
        System.out.println(Thread.currentThread().getName() + ":" + "nihao ");

    }

}

class MyThread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }

        }
    }
}

    class MyThread2 extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                if (i % 2 != 0) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }

            }
        }
    }

